package com.ease.platform.usercontext.vo;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.ease.common.util.Detect;
import com.ease.platform.model.type.IdentityGroup;
import com.ease.platform.model.type.IdentityLocation;
import com.ease.platform.model.type.IdentityUser;

/**
 * http://spring.group.iteye.com/group/blog/815998
 * 
 * @Transient和transient关键字在hibernate中和memcache中应用 将字段标识为transient可以使得POJO的这个字段不被映射到数据库中的字段
 *                                                ,但是如果要将这个POJO存入memcache就会无法存储( 如果这个POJO是实现的Serializable,
 *                                                而不是Externalizable接口自己写的序列化方面实现 )。 只有将字段用注解@Transient的方式标志, 就能实现不映射到数据库
 *                                                ,同时又能序列化到memcache中。 如下代码： Java代码 private transient ProjectInfoState
 *                                                projectInfoState;
 * @Transient private ProjectInfoState projectInfoState;
 */
public class UserContext implements Serializable, Cloneable {

	/**
	 *
	 */
	private static final long serialVersionUID = 9176941891434972231L;

	/**
	 * check with userContext from other system.
	 */
	private String guid;

	/** */
	private String hostName;

	/** */
	private String hostAddress;

	/** */
	private String operator;

	/** */
	private long userId;

	/** */
	private IdentityUser user;

	/** */
	private List<IdentityGroup> groups;

	/** */
	private List<IdentityLocation> maintenanceLocations;

	/** 工单的特殊权限 控制到子区域 */
	private List<IdentityLocation> maintenanceSubareaLocations;

	/** */
	private transient Date operateTime;

	private boolean needValidation = true;

	public Date getOperateTime() {
		// if (null == operateTime) { //对于内部定时功能存在问题
		operateTime = new Date();
		// }
		return operateTime;
	}

	/**
	 * @return the guid
	 */
	public String getGuid() {
		return guid;
	}

	/**
	 * @param guid
	 *            the guid to set
	 */
	public void setGuid(String guid) {
		this.guid = guid;
	}

	/**
	 * @return the hostName
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * @param hostName
	 *            the hostName to set
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	/**
	 * @return the hostAddress
	 */
	public String getHostAddress() {
		return hostAddress;
	}

	/**
	 * @param hostAddress
	 *            the hostAddress to set
	 */
	public void setHostAddress(String hostAddress) {
		this.hostAddress = hostAddress;
	}

	/**
	 * @return the operator
	 */
	public String getOperator() {
		if (Detect.notEmpty(operator)) {
			return operator;
		}
		if (user != null) {
			return user.getUsername();
		}
		return "Default";
	}

	/**
	 * @param operator
	 *            the operator to set
	 */
	public void setOperator(String operator) {
		this.operator = operator;
	}

	/**
	 * @return the userId
	 */
	public long getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(long userId) {
		this.userId = userId;
	}

	/**
	 * @return the user
	 */
	public IdentityUser getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(IdentityUser user) {
		this.user = user;
	}

	/**
	 * @return the groups
	 */
	public List<IdentityGroup> getGroups() {
		return groups;
	}

	/**
	 * @param groups
	 *            the groups to set
	 */
	public void setGroups(List<IdentityGroup> groups) {
		this.groups = groups;
	}

	/**
	 * @param operateTime
	 *            the operateTime to set
	 */
	public void setOperateDate(Date operateDate) {
		this.operateTime = operateDate;
	}

	public List<IdentityLocation> getMaintenanceLocations() {
		return maintenanceLocations;
	}

	public void setMaintenanceLocations(List<IdentityLocation> maintenanceLocations) {
		this.maintenanceLocations = maintenanceLocations;
	}

	public boolean isNeedValidation() {
		return needValidation;
	}

	public void setNeedValidation(boolean needValidation) {
		this.needValidation = needValidation;
	}

	public List<IdentityLocation> getMaintenanceSubareaLocations() {
		return maintenanceSubareaLocations;
	}

	public void setMaintenanceSubareaLocations(List<IdentityLocation> maintenanceSubareaLocations) {
		this.maintenanceSubareaLocations = maintenanceSubareaLocations;
	}

	public UserContext clone() {
		UserContext userContext = null;
		try {
			userContext = (UserContext) super.clone();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException("UserContext  dosenot support cloneMethod");
		}
		return userContext;
	}

}
